package mest.service.hank.impl;

import mest.VO.hank.dataTraffic_model.plan_takematerial_Model;
import mest.dao.Modeling.IemployeeFileDao;
import mest.dao.hank.plan_takematerialMapper;
import mest.dao.hank.warehouse_takematerialMapper;
import mest.entity.plan_productjob_takematerial;
import mest.entity.plan_productjob_takematerial_record;
import mest.entity.plan_takematerial;
import mest.entity.warehouse_takematerial;
import mest.service.Modeling.IemployeeFileService;
import mest.service.hank.PlanProductjobTakematerialRecordService;
import mest.service.hank.PlanProductjobTakematerialService;
import mest.service.hank.PlanTakematerialService;
import mest.util.DateUtil;
import mest.util.hankUtil.Util_Code;
import mest.util.hankUtil.Util_CodeFactory;
import mest.util.mestUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlanTakematerialServiceImpl implements PlanTakematerialService {
    @Resource
    private plan_takematerialMapper ptm;


    @Override
    public int deleteByPrimaryKey(Integer id,String theirDb) {
        return ptm.deleteByPrimaryKey(id, theirDb);
    }

    @Override
    public int insert(plan_takematerial record,String theirDb) {
        return ptm.insert(record, theirDb);
    }

    @Override
    public int insertSelective(plan_takematerial record,String theirDb) {
        return ptm.insertSelective(record, theirDb);
    }

    @Override
    public plan_takematerial selectByPrimaryKey(Integer id,String theirDb) {
        return ptm.selectByPrimaryKey(id, theirDb);
    }

    @Override
    public int updateByPrimaryKeySelective(plan_takematerial record,String theirDb) {
        return ptm.updateByPrimaryKeySelective(record, theirDb);
    }

    @Override
    public int updateByPrimaryKey(plan_takematerial record,String theirDb) {
        return ptm.updateByPrimaryKeySelective(record, theirDb);
    }



    //员工表的Mapper
    @Resource
    private IemployeeFileDao iefd;
    //model查询
    @Override
    public List<Map> queryAllModel(Integer index,
                                   Integer pageSize, String record, Integer state, String theirDb) {
        return ptm.queryAllModel((index - 1) * pageSize, pageSize, record, state, theirDb);
    }

    //model总行数
    @Override
    public int findCount(String record, Integer state, String theirDb) {
        return ptm.findCount(record,state, theirDb);
    }


    //仓库流的Mapper
    @Resource
    private warehouse_takematerialMapper wtM;



    /**
     * 更改计划领料单状态的方法(做错逻辑)
     * 0-未完成
     * 1-已完成
     * 2-未按预期领料
     * 更改订单同时并且生成.. warehouse_takematerial表资料
     *
     * @param record
     * @return
     */

    @Override
    public Map planTakeMaterial_changeState(plan_takematerial record, String theirDb) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        //1.判断传回来的值是否为空
        if (record == null || record.getId() == null) {
            modelMap.put("resultMsg", Util_Code.Null_CODE);
            return modelMap;
        } else {
            try {
                //2.更改..planTakeMaterial 的状态
                int num = ptm.updateByPrimaryKeySelective(record, theirDb);
                //3.插入成功后
                if (num > 0) {
                    plan_takematerial plan_takematerial = ptm.selectByPrimaryKey(record.getId(),theirDb);
                    //判断plan_takematerial 对象大于>0的时候才插入 仓库流
                    if (!ObjectUtils.isEmpty(plan_takematerial)||plan_takematerial.getFinishStatus()>0){
                        //4.new warehouse_takematerial 对象 并把值set进去
                        warehouse_takematerial model = new warehouse_takematerial();
                        //插入时间
                        model.setTime(DateUtil.getDateTimeToDate());
                        //插入ID
                        model.setPtId(record.getId());
                        //插入.生成随机订单编号
                        String TableNumber = Util_CodeFactory.create_warehouse_takematerial_CODE("1");
                        model.setTableNumber(TableNumber);
                        model.setFinishStatus(0);
                        model.setDescription("备注");
                        //5.动态插入数据
                        int wtmNum = wtM.insertSelective(model, theirDb);
                        if (wtmNum>0){
                            modelMap.put("result","更新数量:"+num+", 插入数量:"+wtmNum);
                            modelMap.put("resultMsg", Util_Code.Success_CODE);
                        }else {
                            modelMap.put("result","wtm插入失败");
                            modelMap.put("resultMsg", Util_Code.Fail_CODE);
                        }

                    }else {
                        modelMap.put("result","wtm插入失败");
                        modelMap.put("resultMsg", Util_Code.Fail_CODE);
                    }

                } else {
                    modelMap.put("result", num);
                    modelMap.put("resultMsg", "更改失败");
                }
            } catch (Exception e) {
                modelMap.put("errMsg", e.toString());
                modelMap.put("resultMsg", "异常");
            }
        }

        return modelMap;
    }

    @Override
    public int insertResult_id(Map<String, Object> map) {
        ptm.insertResult_id(map);
        //System.out.println("record.getId():"+record.getId());
        return Integer.valueOf(""+map.get("id"));
    }

	@Override
	public int updateByMap(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return ptm.updateByMap(map);
	}

	@Override
	public int existBom(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return ptm.existBom(map);
	}


}